package controllers

import (
	"beego_benchu/services/mq"
	"strconv"
	"time"

	"github.com/beego/beego/v2/server/web"
)

type MqDemoController struct {
	web.Controller
}

// 简单模式和work 工作模式 push方法
func (this *MqDemoController) GetMq() {
	go func() {
		count := 0
		for {
			mq.Publish("", "fyouku_demo", "hello"+strconv.Itoa(count))
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("hello")
}

//发布订阅模式
func (this *MqDemoController) GetMqEx() {
	go func() {
		count := 0
		for {
			mq.PublishEx("fyouku.demo.fanout", "fanout", "", "hello"+strconv.Itoa(count))
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("hello")
}

// 路由模式
func (this *MqDemoController) GetDirect() {
	go func() {
		count := 0
		for {
			if count%2 == 0 {
				mq.PublishEx("fyouku.demo.direct", "direct", "two", "direct"+strconv.Itoa(count))
			} else {
				mq.PublishEx("fyouku.demo.direct", "direct", "one", "direct"+strconv.Itoa(count))
			}
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("direct")
}

// Topic 主题模式 (最常用的)
func (this *MqDemoController) GetTopic() {
	go func() {
		count := 0
		for {
			if count%2 == 0 {
				mq.PublishEx("fyouku.demo.topic", "topic", "fyouku.video", "video"+strconv.Itoa(count))
			} else {
				mq.PublishEx("fyouku.demo.topic", "topic", "fyouku.user", "user"+strconv.Itoa(count))
			}
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("direct")
}

// 死信队列 A队列push
func (this *MqDemoController) GetDlx() {
	go func() {
		count := 0
		for {
			if count%2 == 0 {
				mq.PublishDlx("fyouku.dlx.a", "dlx"+strconv.Itoa(count))
			} else {
				mq.PublishDlx("fyouku.dlx.a", "dlx"+strconv.Itoa(count))
			}
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("direct")
}

// 死信队列 B队列push
func (this *MqDemoController) GetTwoDlx() {
	go func() {
		count := 0
		for {
			mq.PublishEx("fyouku.dlx.b", "fanout", "", "dlxtwo"+strconv.Itoa(count))
			count++
			time.Sleep(1 * time.Second)
		}
	}()
	this.Ctx.WriteString("dlxtwo")
}
